#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re


#在用匹配表达式之前，要将中文字符用utf-8编码

def abstract_tran_info(info,lang):
    reg_transaction_amount = re.compile(r".*交易金额：.*?(\d+[,\.]\d+)\s(\w{3})")
    reg_invoice_no         = re.compile(r".*账单号：([_\w-]*)")
    reg_transaction_no     = re.compile(r".*您的交易号：\s?(\w*)")
    reg_case_id            = re.compile(r".*事件编号：\s?([-\w]*)")

    reg_transaction_amount_en  = re.compile(r".*Transaction amount:.*?(\d+[,\.]\d+)\s(\w{3})")
    reg_invoice_no_en          = re.compile(r".*Invoice ID: ([_\w-]*)")
    reg_transaction_no_en      = re.compile(r".*Your transaction ID:\s?(\w*)")
    reg_case_id_en             = re.compile(r".*Case number:\s?([-\w]*)")

    list_info = info.split('\n')
 
    transaction_info = {'amount':'','currency':'','invoice_no':'NoOrderNO','transaction_no':'','case_id':''}

    if lang == 'en' :
        for i in list_info:
            m = reg_transaction_amount_en.match(i)
            if m :
                 transaction_info['amount'],transaction_info['currency']= m.groups()
                 continue
            else:
                m = reg_invoice_no_en.match(i)
                if m :
                    #此处正则表达式分组不明白，但能达到效果
                     transaction_info['invoice_no'] = m.groups()[0]
                     continue
                else:
                     m = reg_transaction_no_en.match(i)
                     if m:
                         transaction_info['transaction_no'] = m.groups()[0]
                         continue
                     else:
                         m = reg_case_id_en.match(i)
                         if m : 
                             transaction_info['case_id'] = m.groups()[0]
    else :
        for i in list_info:
            m = reg_transaction_amount.match(i)
            if m :
                 transaction_info['amount'],transaction_info['currency']= m.groups()
                 continue
            else:
                m = reg_invoice_no.match(i)
                if m :
                    #此处正则表达式分组不明白，但能达到效果
                     transaction_info['invoice_no'] = m.groups()[0]
                     continue
                else:
                     m = reg_transaction_no.match(i)
                     if m:
                         transaction_info['transaction_no'] = m.groups()[0]
                         continue
                     else:
                         m = reg_case_id.match(i)
                         if m : 
                             transaction_info['case_id'] = m.groups()[0]
        #有些交易无订单号
        
    return transaction_info
if __name__ == '__main__' : #注意这里的名字是双下划线
    fp = open('D:\Emails\haha.txt','r')
    
    info = fp.read()

    m = {}
    m = abstract_tran_info(info,'ch')

    print m


